package com.wbao.dianniu.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.wbao.dianniu.R;
import com.wbao.dianniu.adapter.QAInfoAdapter;
import com.wbao.dianniu.data.QaResponse;
import com.wbao.dianniu.listener.IQaListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.update.QaDataChangeManager;
import com.wbao.dianniu.update.QaDataChangeManager.IQaDataListener;
import com.wbao.dianniu.manager.QaManager;
import com.wbao.dianniu.update.ErrorDialogHelper;
import com.wbao.dianniu.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 我发的问答
 *
 * @author yejianjun
 */
public class SendQaFragment extends Fragment implements IQaListener, OnClickListener, IQaDataListener {

    private LinearLayout noDataLayout;
    private Button sendBtn;
    private PullToRefreshListView pullListView;
    private ListView mListView;
    private QaManager manager;
    private int TYPE = 3;
    private final int PAGE_COUNTS = 10;
    private QAInfoAdapter adapter;
    private List<QaResponse> mList;
    private Dialog dialog;
    public static final int TYPE_MYSELF = 1;
    public static final int TYPE_ALL = 2;
    private static int user_type = TYPE_MYSELF;
    private static Integer accountId;
    private TextView noDataTV;
    private QaDataChangeManager qaDataChangeManager;

    public static SendQaFragment instance(Context context,int type, int id) {
        SendQaFragment view = new SendQaFragment();
        user_type = type;
        accountId = id;
        return view;
    }

    @Override
    @Nullable
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_send_qa, null);
        adapter = new QAInfoAdapter(getActivity());
        qaDataChangeManager = QaDataChangeManager.getInstance();
        qaDataChangeManager.addQaChangeListener(this);
        initView(view);
        requestData();
        return view;
    }

    private void requestData() {
        manager = new QaManager(getActivity());
        manager.addQaListener(this);
        reqData("down");
    }

    private void initView(View view) {
        noDataLayout = (LinearLayout) view.findViewById(R.id.no_data_layout);
        sendBtn = (Button) view.findViewById(R.id.send_qa_button);
        noDataTV = (TextView) view.findViewById(R.id.no_data_tv);

        pullListView = (PullToRefreshListView) view.findViewById(R.id.list_send_qa);
        pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
        pullListView.setOnRefreshListener(onRefreshListener);
        mListView = pullListView.getRefreshableView();
        registerForContextMenu(mListView);
        mListView.setAdapter(adapter);
        sendBtn.setOnClickListener(this);
        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                Utils.intoQaDetailActivity(getContext(), (QaResponse) adapter.getItem(position - 1));
            }
        });
    }

    OnRefreshListener onRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            // 上拉刷新 加载请求数据
            pullUpRequest();
        }
    };

    // 上拉刷新数据请求
    private void pullUpRequest() {
        // 没有搜索结果 或者数据为空 上拉没有效果
        reqData("up");
    }

    private void reqData(String refresh) {
        manager.reqData(accountId, "", TYPE, 0, PAGE_COUNTS, adapter.getLastId(), refresh);
    }

    @Override
    public void onQaFailure(int errorCode, String errorMsg) {
        pullListView.onRefreshComplete(false, false);
    }

    @Override
    public void onQaSuccess(List<QaResponse> list) {
        pullListView.onRefreshComplete(false, false);
        if (null != list) {
            adapter.addBackwardList(list);
            mList = adapter.getList();
            adapter.notifyDataSetChanged();
            changeNoData(mList.size());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_qa_button:
                if (checkLevel()) {
                    Intent ask = new Intent();
                    ask.setClass(getActivity(), AskActivity.class);
                    startActivity(ask);
                } else {
                    ErrorDialogHelper.getInstance().showPerfectAuth(getContext());
                }
                break;

            default:
                break;
        }
    }

    public boolean checkLevel() {
        boolean ret = false;
        if (GlobalContext.getPerfectData()) {
            // 提示用户完成认证 必须达到一级认证才能提问
            ret = true;
        }
        return ret;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(null != qaDataChangeManager){
            qaDataChangeManager.removeQaChangeListener(this);
        }
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }
        if (null != manager) {
            manager.removeQaListener(this);
        }
    }

    @Override
    public void removeOneData(int questId) {
        adapter.removeOneData(questId);
        mList = adapter.getList();
        changeNoData(mList.size());
    }

    @Override
    public void priseNotify(int questId, int isGood, int goodCounts) {
        // TODO Auto-generated method stub
        adapter.priseNotify(questId, isGood, goodCounts);
    }

    @Override
    public void replyNotify(int questId, int answerCounts) {
        // TODO Auto-generated method stub
        adapter.replyNotify(questId, answerCounts);
    }

    @Override
    public void visitorUpdate(int questId, int visitorCounts) {
        adapter.visitorUpdate(questId, visitorCounts);
    }

    @Override
    public void addOneData(QaResponse addData) {
        // TODO Auto-generated method stub
        noDataLayout.setVisibility(View.GONE);
        noDataTV.setVisibility(View.GONE);
        List<QaResponse> list = new ArrayList<QaResponse>();
        list.add(addData);
        adapter.addForwardList(list);
        adapter.notifyDataSetChanged();
    }

    private void changeNoData(int size){
        if (size == 0) {
            if(user_type == TYPE_MYSELF){
                noDataLayout.setVisibility(View.VISIBLE);
                noDataTV.setVisibility(View.GONE);
            }else if(user_type == TYPE_ALL){
                noDataLayout.setVisibility(View.GONE);
                noDataTV.setVisibility(View.VISIBLE);
            }
        } else {
            noDataLayout.setVisibility(View.GONE);
            noDataTV.setVisibility(View.GONE);
        }
    }

}
